# szdzld insert
import csv
import os
import re


def is_number(num):
    """"""
    try:
        float(num)
        return True
    except ValueError:
        pass
    try:
        import unicodedata
        unicodedata.numeric(num)
        return True
    except (TypeError, ValueError):
        pass
    return False


def count_rows(csv_file_path):
    with open(csv_file_path, 'r', newline='') as csv_file:
        reader = csv.reader(csv_file)
        row_count = 0
        for row in reader:
            row_count += 1
        return row_count - 1


def write_to_csv(file_path, data):
    with open(file_path, 'a', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(data)


def is_file(key):
    for root, ds, fs in os.walk('data'):
        for f in fs:
            if key in f:
                return False
            else:
                return True


def file_data(filename, column, search, row_name):
    with open(filename, 'r', newline='') as file:
        reader = csv.DictReader(file)
        for row in reader:
            if row[column] == search:
                if row_name in row:
                    return row[row_name]
        return None


def country():
    print("0: China\n"
          "1: USA\n"
          "2: Russia\n"
          "3: Europe\n"
          "4: India\n"
          "5: Japan\n"
          "6: South Korea\n"
          "7: North Korea\n"
          "8: Iran\n"
          "9: Israel")

    country_dict = {
        '0': 'China',
        '1': 'USA',
        '2': 'Russia',
        '3': 'Europe',
        '4': 'India',
        '5': 'Japan',
        '6': 'South Korea',
        '7': 'North Korea',
        '8': 'Iran',
        '9': 'Israel',
    }
    while True:
        country_code = input("Enter the country code:")
        if country_code in country_dict:
            return country_dict[country_code]
        else:
            print("Invalid parameter")


def is_orbit(ap, pe):
    # check ap and pe

    if pe <= ap:
        return True
    else:
        return False


def return_match(string1, string2, data):
    pattern = rf"{re.escape(string1)}(.*?){re.escape(string2)}"
    match_data = re.findall(pattern, data)
    match_data_1 = str(match_data).replace("['", '')
    match = str(match_data_1).replace("']", '')
    return match
